import pandas as pd
from tools.utils import load_config
from tools.utils import loc_collection
from tools.utils import get_ts_code_to_chinese_name
from pyecharts.charts import Line, Grid
from pyecharts import options as opts
from pyecharts.globals import ThemeType

sbl = "Y"
cfg = load_config()
ts_to_chinese = get_ts_code_to_chinese_name(cfg)

a = list(ts_to_chinese.keys())
symbol_df = pd.DataFrame(list(loc_collection("ts_code_future", sbl).find()))
selected_df = symbol_df.loc[:, ['trade_date', 'close', 'ts_code']]

dic = {}
for k, v in selected_df.groupby(['ts_code']):
    v.index = v.trade_date
    print(k, v.shape)
    if len(v) < 500:
        if ("01" in k) or ('05' in k) or ('09' in k):
            dic[k] = v.close

df = pd.DataFrame(dic)
t = [pd.to_datetime(str(s)) for s in df.index]

grid = Grid()
line = Line(init_opts=opts.InitOpts(width="1850px", height="850px", theme=ThemeType.CHALK))
line.add_xaxis(t)
for k, v in df.iteritems():
    line.add_yaxis(series_name=k, y_axis=v.to_list(),
                   label_opts=opts.LabelOpts(is_show=False))
line.set_global_opts(
    datazoom_opts=[
        opts.DataZoomOpts(start_value=0, end_value=0, orient="horizontal"),
        opts.DataZoomOpts(start_value=0, end_value=100, orient="vertical")
    ],
    legend_opts=opts.LegendOpts(is_show=True, pos_bottom='9%', orient='horizontal')
)
grid.add(line, grid_opts=opts.GridOpts(pos_top="40%"))

line.render(f'{sbl}.html')
